Backtest: Portfolio Returns





Kerry Back

  • Form a portfolio each month based on the predictions
  • How many stocks?
  • Short sales? 150/50, 130/30, 100/100, 100/0?
  • How many stocks on long and short sides?
  • How to allocate money across stocks on long and short sides?
    • Equal weight?
    • Weight based on predictions? More on best, etc.?

Portfolio returns

  • Important: before transforming return in preprocessing step, save actual return with a different name (or use different name for the transformed return).
  • Example: df[“actual”] = df[“ret”]

Example: equally weighted

numlong = 100
numshort = 100

df["rank_from_top"] = df.groupby("date").predict.rank(
  method="first", 
  ascending=False
)
df["long"] = df.rank_from_top <= numlong

df["rank_from_bottom"] = df.groupby("date").predict.rank(
  method="first"
)
df["short"] = df.rank_from_bottom <= numshort

Monthly returns

df = df[df.index.get_level_values("date") >= dates[0]]

long_ret = df.groupby("date").apply(
    lambda d: (d.long*d.actual).mean()
)
short_ret = df.groupby("date").apply(
    lambda d: (d.short*d.actual).mean()
)

Example: 150/50

ret = 1.5*long_ret - 0.5*short_ret
ret
date
2005-01    0.016546
2005-02    0.022419
2005-03    0.004282
2005-04    0.008603
2005-05    0.002557
             ...   
2021-11    0.024575
2021-12    0.021031
2022-01    0.015138
2022-02    0.007181
2022-03   -0.001958
Length: 207, dtype: float64